package com.lmabbe.core.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.core.common.converter.SysDicConverter;
import com.lmabbe.core.common.converter.SysDicItemConverter;
import com.lmabbe.core.common.dto.SysDicDTO;
import com.lmabbe.core.common.dto.SysDicItemDTO;
import com.lmabbe.core.common.entitys.SysDic;
import com.lmabbe.core.common.entitys.SysDicItem;
import com.lmabbe.core.common.interfaces.SysDicService;
import com.lmabbe.core.common.query.SysDicItemQuery;
import com.lmabbe.core.common.query.SysDicQuery;
import com.lmabbe.core.common.vo.SysDicVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统字典表管理
 *
 * @author lmabbe
 */
@Api(tags = "系统字典表管理")
@RestController
@RequestMapping("sysDic")
@RequiredArgsConstructor
public class SysDicController {

    private final SysDicConverter sysDicConverter;
    private final SysDicItemConverter sysDicItemConverter;
    @DubboReference
    private SysDicService sysDicService;

    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<SysDicVO>> list() {
        List<SysDic> list = sysDicService.list();
        return R.success(sysDicConverter.list2Vos(list));
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<SysDicVO>> list(SysDicQuery query) {
        IPage page = sysDicService.selectPage(query);
        page.setRecords(sysDicConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<SysDicVO> getById(@PathVariable Long id) {
        return R.success(sysDicConverter.entity2Vo(sysDicService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody SysDicDTO dto) {
        this.sysDicService.save(sysDicConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{dicId}")
    public R update(@RequestBody SysDicDTO dto, @PathVariable Long dicId) {
        this.sysDicService.updateById(sysDicConverter.dto2Entity(dto, dicId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.sysDicService.removeById(id);
        return R.success();
    }


    /**
     * 获取字典条目列表
     *
     * @return
     */
    @Operation(summary = "获取字典条目列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "dicId", value = "字典ID")
    })
    @GetMapping("/{dicId}/items")
    public R items(@PathVariable Long dicId, SysDicItemQuery query) {
        query.setDicId(dicId);
        IPage<SysDicItem> resources = sysDicService.items(dicId, query);
        return R.page(resources);
    }

    /**
     * 获取字典条目详情
     *
     * @param itemId 条目ID
     * @return
     */
    @Operation(summary = "获取字典条目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "itemId", value = "条目ID")
    })
    @GetMapping("/item/{itemId}")
    public R item(@PathVariable Long itemId) {
        SysDicItem item = sysDicService.item(itemId);
        return R.success(item);
    }

    /**
     * 删除字典条目
     */
    @Operation(summary = "删除字典条目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "itemId", value = "条目ID")
    })
    @DeleteMapping("item/{itemId}")
    public R delItem(@PathVariable Long itemId) {
        sysDicService.delItem(itemId);
        return R.success();
    }

    /**
     * 新增字典条目
     */
    @Operation(summary = "新增字典条目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "itemId", value = "条目ID"),
            @ApiImplicitParam(name = "item", value = "条目详情")
    })
    @PostMapping("{dicId}/item")
    public R addItem(@PathVariable Long dicId, @RequestBody SysDicItemDTO itemDTO) {
        SysDicItem item = sysDicItemConverter.dto2Entity(itemDTO);
        item.setDicId(dicId);
        sysDicService.addItem(item);
        return R.success();
    }

    /**
     * 修改字典条目
     */
    @Operation(summary = "修改典条目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "itemId", value = "条目ID"),
            @ApiImplicitParam(name = "item", value = "条目详情")
    })
    @PutMapping("{dicId}/item/{itemId}")
    public R editItem(@PathVariable Long dicId, @RequestBody SysDicItemDTO itemDTO, @PathVariable Long itemId) {
        SysDicItem item = sysDicItemConverter.dto2Entity(itemDTO, itemId);
        item.setDicId(dicId);
        sysDicService.editItem(item);
        return R.success();
    }
}